/*
 *  Copyright 2011.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.queryhandler.n.client.ui.actions;

import java.awt.event.ActionEvent;
import java.util.Locale;
import java.util.ResourceBundle;
import org.opu.queryhandler.n.util.ConfigPath;
import org.opu.queryhandler.n.util.Logger;
import org.opu.queryhandler.n.util.Options;
import org.opu.queryhandler.n.util.ResourceHandle;

/**
 *
 */
public abstract class AbstractAction extends javax.swing.AbstractAction{
    public static final String ACTIONLABELS = 
            "org.opu.queryhandler.n.actions.ActionLabels";
    protected ResourceBundle boundle;
    protected Logger logger = Logger.getInstance();

    public AbstractAction() {
        reset();
    }

    public final void reset(){
        setIcon();
        setText(ConfigPath.getInstance().getLocale());
    }

    protected void setIcon() {
        ResourceHandle.IconType small = ResourceHandle.IconType.small;
        ResourceHandle.IconType large = ResourceHandle.IconType.medium;
        putValue(SMALL_ICON, ResourceHandle.getIcon(getIconName(), small));
        putValue(LARGE_ICON_KEY, ResourceHandle.getIcon(getIconName(), large));
    }

    protected void setText(Locale locale) {
        boundle = ResourceBundle.getBundle( ACTIONLABELS, locale);
        safetyPutValue(NAME, getName());
        safetyPutValue(SHORT_DESCRIPTION, getName());
        safetyPutValue(LONG_DESCRIPTION, getName());
    }

    protected void safetyPutValue(String name, String defVal){
        putValue(name, getSafety(getName() + "." + name, defVal));
    }

    protected String getSafety(String name, String defName){
        try {
            return boundle.getString(name);
        } catch (Exception e) {
            logger.error(getClass(), "Can't init property " + name);
            return defName;
        }
    }

    protected String getTextResource(String name){
        return boundle.getString(getName() + "." + name);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        logger.info(getClass(), "Action [" + getName() + "] start (" + e + ")");

        try {
            action(e);
        } catch (Exception ex) {
            onFailure(ex);
        }

        logger.info(getClass(), "Action [" + getName() + "] end");
    }

    public abstract String getName();

    public abstract void action(ActionEvent e) throws Exception;

    protected void onFailure(Exception ex){
        logger.error(getClass(), ex);
        Options.error(ex);
    }

    public String getIconName(){
        return getName() + ".png";
    }

    public static String getText(String name){
        try {
            return ResourceBundle.getBundle(ACTIONLABELS,
                    ConfigPath.getInstance().getLocale()).getString(name);
        } catch (Exception e) {
            Logger.getInstance().error(AbstractAction.class,
                    "Can't init property " + name);
            return name;
        }
    }
}
